﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyNhaSach.param
{
    public partial class FrmDocSequence : DevComponents.DotNetBar.Office2007Form
    {
        DocSequenceBUS _docSeqBUS;
        string _oldValue;

        public FrmDocSequence()
        {
            InitializeComponent();
            dtgDocSequence.AutoGenerateColumns = false;
            _docSeqBUS = new DocSequenceBUS();
        }

        private void FrmDocSequence_Load(object sender, EventArgs e)
        {
            this.VisibleChanged += new EventHandler(FrmDocSequence_VisibleChanged);
            
        }

        void FrmDocSequence_VisibleChanged(object sender, EventArgs e)
        {
            LoadDocSequenceList();
        }

        private void LoadDocSequenceList()
        {
            List<DocSequenceDTO> ListDocSeq = _docSeqBUS.GetListDocSequence();
            dtgDocSequence.DataSource = ListDocSeq;
            Global.GenerateNumber(dtgDocSequence, colNumber.Index);
        }

        private void dtgDocSequence_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            _oldValue = Global.stringParse( dtgDocSequence[e.ColumnIndex, e.RowIndex].Value);
        }

        private void dtgDocSequence_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colStart.Index)
            {
                string value = Global.stringParse( dtgDocSequence[e.ColumnIndex, e.RowIndex].Value);
                if (value.Length <= 0 || value.Length > 2)
                {
                    SetMessage("Kí tự bắt đầu phải có 2 kí tự.", false);
                    dtgDocSequence[e.ColumnIndex, e.RowIndex].Value = _oldValue;
                }
            }
            //else if (e.ColumnIndex == colSeparator.Index)
            //{
            //    string value = Global.stringParse(dtgDocSequence[e.ColumnIndex, e.RowIndex].Value);
            //    if (value.Length != 1)
            //    {
            //        SetMessage("Dấu phân cách phải có 1 kí tự.", false);
            //        dtgDocSequence[e.ColumnIndex, e.RowIndex].Value = _oldValue;
            //    }
            //}
            //else if (e.ColumnIndex == colMiddle.Index)
            //{
            //    string value = Global.stringParse(dtgDocSequence[e.ColumnIndex, e.RowIndex].Value);
            //    if (value.Length != 4)
            //    {
            //        SetMessage("Chuỗi ở giữa phải có 4 kí tự.", false);
            //        dtgDocSequence[e.ColumnIndex, e.RowIndex].Value = _oldValue;
            //    }
            //}
            else if (e.ColumnIndex == colLength.Index)
            {
                int num = Global.intParse(dtgDocSequence[e.ColumnIndex, e.RowIndex].Value);
                if (num <= 0)
                {
                    SetMessage("Độ dài số phải là số và lớn hơn 0.", false);
                    dtgDocSequence[e.ColumnIndex, e.RowIndex].Value = _oldValue;
                }
            }
        }

        private void SetMessage(string mess, bool isSuccess)
        {
            Global.SetMessage(lblMessage, mess, isSuccess);
        }

        private void dtgDocSequence_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = false;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            List<DocSequenceDTO> listDocSeq = new List<DocSequenceDTO>();

            foreach (DataGridViewRow row in dtgDocSequence.Rows)
            {
                DocSequenceDTO docSeq = new DocSequenceDTO();
                docSeq.DocSequenceID = Global.intParse(row.Cells[colDocSequenceID.Index].Value);
                docSeq.DocSequenceText = Global.stringParse(row.Cells[colDocSeqName.Index].Value);
                docSeq.DocSequenceCode = Global.stringParse(row.Cells[colStart.Index].Value).ToUpper();
                docSeq.Separator = Global.stringParse(row.Cells[colSeparator.Index].Value);
                docSeq.Middle = Global.stringParse(row.Cells[colMiddle.Index].Value);
                docSeq.NumLength = Global.intParse(row.Cells[colLength.Index].Value);

                listDocSeq.Add(docSeq);
            }

            if(_docSeqBUS.UpdateDocSequence(listDocSeq))
            {
                SetMessage("Cập nhật mã số chứng từ thành công.",true);
            }
            else
            {
                SetMessage("Có lỗi trong quá trình làm việc, vui lòng thử lại.",false);
            }

            LoadDocSequenceList();
        }
    }
}
